import { request as baseRequest } from 'umi';
import { message } from 'antd';
import type { ResponseData } from '@/models';

export const request = <T extends unknown>(
  ...[url, options]: Parameters<typeof baseRequest>
): Promise<T> =>
  baseRequest<ResponseData<T>>(url, options).then(
    ({ data, msg, code }) =>
      new Promise((resolve, reject) => {
        if (code === 1) {
          message.error(msg);
          reject(msg);
        } else {
          resolve(data);
        }
      }),
  );
/** 下载 */
export const download = (url: string) => {
  window.open(url, '_blank');
}; /** bolb文件下载 */
export const bolbFileDownload = (name: string) => (blob: Blob) => {
  const url = window.URL.createObjectURL(blob);
  const a = document.createElement('a');
  a.href = url;
  a.download = name;
  a.click();
};
